package com.ww.java7.path;

import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author wanggw
 * @Date 2023年03月01 0:16
 */
public class PathDemo {

    public static void main(String[] args) {
        Path path = Paths.get("D:\\test\\logs\\log");
        // 完整路径：D:\test\logs\log
        System.out.println("完整路径：" + path.toString());

        Path pathParent = path.getParent();
        // 父级路径：D:\test\logs
        System.out.println("父级路径：" + pathParent.toString());

        Path pathRoot = path.getRoot();
        // 根目录：D:\
        System.out.println("根目录：" + pathRoot.toString());

        int pathNameCount = path.getNameCount();
        // 目录深度：3
        System.out.println("目录深度：" + pathNameCount);

        Path pathIndex3 = path.getName(2);
        // 第三级目录：log
        System.out.println("第三级目录：" + pathIndex3);

        Path subPath = path.subpath(1, 3);
        // 第2级目录到第3级目录（包左不包右）：logs\log
        System.out.println("第2级目录到第3级目录（包左不包右）：" + subPath.toString());

        Path pathResolveSibling = path.resolveSibling("PathDemo.java");
        // 父目录开始拼接参数：D:\test\logs\PathDemo.java
        System.out.println("父目录开始拼接参数：" + pathResolveSibling.toString());

        // resolve把当前路径当作父路径，参数作为子目录或者文件
        Path pathResolve = Paths.get("D:\\test\\logs\\log").resolve("PathDemo.java");
        // 当前目录拼接后的目录：D:\test\logs\log\PathDemo.java
        System.out.println("当前目录拼接后的目录：" + pathResolve.toString());

        Path path1 = Paths.get("D:/test/logs/");
        Path path2 = Paths.get("D:/test/logs/log/PathDemo.java");
        Path path3 = path1.relativize(path2);
        // 相对路径：log\PathDemo.java
        System.out.println("相对路径：" + path3.toString());
    }
}
